//
//  VideoViewController.m
//  TouTiao
//
//  Created by 柴旭 on 16/9/12.
//  Copyright © 2016年 Match. All rights reserved.
//  测试push＋commit

#import "VideoViewController.h"
#import "UIBarButtonItem+buttonItem.h"




@interface VideoViewController ()<UIScrollViewDelegate>
// 添加内容的scrollerView
@property(nonatomic,strong)UIScrollView *scrollerView;
// 用来存放标题的
@property(nonatomic,strong)NSArray *titles;
// 添加标题的view
@property(nonatomic,strong)UIScrollView *titleScroll;
//上面的十个按钮
@property(nonatomic,strong)UIButton *button;
// 记录上次选中的按钮
@property(nonatomic,strong)UIButton *UpButton;

@property (nonatomic,strong) UIView * view1;

@end

@implementation VideoViewController
CGFloat w = 60;
CGFloat margin = 15;//每个按钮之间的间距
CGFloat leftMargin = 10;//离屏幕的间距

/*
#pragma mark 当视图被驳回的时候,覆盖或以其他方式隐藏
-(void)viewWillDisappear:(BOOL)animated{
    self.navigationController.navigationBar.subviews.firstObject.alpha = 1;
}
#pragma mark 当视图将要显示的时候被调用
-(void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBar.subviews.firstObject.alpha = 0;
}
 */

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addScrollerView];
    [self BlackToWhite];
   
    [self addItem];
    //加载子控制器
    [self addChildController];
    // 加载内容
    [self addScrollerView];
    [self addItem];
     // 一进“精华”界面就选中第一个"推荐"按钮
    [self addSelectFirstTitle];
    //设置代理
    self.scrollerView.delegate = self;
 
    //把上面的状态栏设置成黑底白字
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    

}


#pragma mark 加载那十个子控制器
- (void)addChildController{

    [self addChildViewController:[[SuggstController alloc]init]];
    [self addChildViewController:[[FunnyController alloc]init]];
    [self addChildViewController:[[MusicController alloc]init]];
    [self addChildViewController:[[WatchWorldController alloc]init]];
    [self addChildViewController:[[GameController alloc]init]];
    [self addChildViewController:[[LovelyController alloc]init]];
    [self addChildViewController:[[EntertainController alloc]init]];
    [self addChildViewController:[[LoveLiveController alloc]init]];
    [self addChildViewController:[[ShadowController alloc]init]];
    [self addChildViewController:[[OpeneyesController alloc]init]];
}



#pragma mark 添加上面的ScrollerView
- (void)addScrollerView{
    _scrollerView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, UIScreenW, UIScreenH)];
    _scrollerView.backgroundColor = [UIColor greenColor];
    
    //计算出加载到控制器上的子控制器的个数（10个）
    NSInteger count = self.childViewControllers.count ;
    int i = 0;
    for (; i<count; i++) {
        self.childViewControllers[i].view.frame = CGRectMake(i*UIScreenW, 0, UIScreenW, UIScreenH);
        [self.scrollerView addSubview:self.childViewControllers[i].view];
    }
    
    self.scrollerView.contentSize = CGSizeMake(count*UIScreenW, 0);
    self.scrollerView.pagingEnabled = YES;
    self.scrollerView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:self.scrollerView];
    
}
#pragma mark 添加导航栏标题（右边的搜索按钮）
- (void)addItem{
    //添加上面的🔍
    UIBarButtonItem *itemR = [UIBarButtonItem barButtonItemWith:[UIButton buttonWithType:UIButtonTypeCustom] addTarget:self action:@selector(search) andImage:@"detail_search_icon_night@2x" andHighlighted:@"detail_search_icon@2x"];
    self.navigationItem.rightBarButtonItem = itemR;//把它加载到右面
    
    
    _titleScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, UIScreenW, 35)];
    _titleScroll.backgroundColor = [[UIColor whiteColor]colorWithAlphaComponent:0.5];
//    [self.view addSubview:_titleScroll];
    _titles = @[@"推荐",@"逗比剧",@"音乐台",@"看天下",@"游戏",@"萌萌哒",@"最娱乐",@"爱生活",@"掠影",@"开眼"];
    
    CGFloat y = 0;
    CGFloat h = self.titleScroll.frame.size.height;
    int i =0;
    for (; i<self.titles.count; i++) {
        _button = [UIButton buttonWithType:UIButtonTypeCustom];//按钮的样式
        [_button setTitle:self.titles[i] forState:UIControlStateNormal];//标题名称
        [_button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];//标题颜色
        [_button setTitleColor:[UIColor redColor] forState:UIControlStateSelected];//标题选中状态颜色
        [_button setBackgroundColor:[UIColor clearColor]];//按钮背景的view的颜色
        _button.frame = CGRectMake(i*(w+margin)+leftMargin, y, w, h);//按钮的大小
        [_button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        _button.tag = i;
        
        [self.titleScroll addSubview:_button];
    }
    self.titleScroll.contentSize =CGSizeMake(i*(w+margin)+leftMargin*2, 0);
    // 隐藏滚动条
    self.titleScroll.showsHorizontalScrollIndicator = NO;
    self.navigationItem.titleView = self.titleScroll;
    
}
#pragma mark 标题按钮的监听事件
-(void)buttonClick:(UIButton *)sender{
    self.UpButton.selected = NO;
    sender.selected = YES;
    self.UpButton = sender;//记录上次选中的按钮（目的是让上次选中的变成原来的颜色）
    //让ScrollView滚动 就得改变contentOffset
    self.scrollerView.contentOffset = CGPointMake(sender.tag*UIScreenW, 0);
}

#pragma mark  一进“视频”界面就选中第一个"推荐"按钮
- (void)addSelectFirstTitle{
    
    [self buttonClick:self.titleScroll.subviews.firstObject];
}




- (void)search{
    NSLog(@"%s",__func__);
}


#pragma mark scrollView的代理方法-目的是为了让那十个控制器，页面滑动的时候，上面的标题栏也跟着滑动
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{  //拖拽停止就会调用该方法
    
    //计算偏移量
    CGFloat length = scrollView.contentOffset.x;
    NSInteger num = length/UIScreenW;//计算是第几个view
    [self buttonClick:self.titleScroll.subviews[num]];
    if (num>=2) {
        self.titleScroll.contentOffset = CGPointMake((num-2)*_button.bounds.size.width, 0);
    }
}
#pragma mark 状态栏
-(void)BlackToWhite{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0,UIScreenW, -20)];
    view.backgroundColor = [UIColor blackColor];
    [self.navigationController.navigationBar addSubview:view];
}



@end
